package com.social.filter;

import cn.hutool.core.util.StrUtil;
import com.social.utils.AESUtil;
import lombok.Data;
import org.apache.commons.codec.binary.Base64;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.web.authentication.www.BasicAuthenticationFilter;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.http.HttpFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.concurrent.TimeUnit;

import static com.social.constant.RedisConstants.*;
import static com.social.utils.RSAUtil.decryptByPrivateKey;

/**
 * @BelongsProject: social
 * @BelongsPackage: com.social.filter
 * @Author: zhm
 * @CreateTime: 2022-12-11  16:37
 * @Description: TODO
 * @Version: 1.0
 */
@Data
@Component
public class EncryptFilter{

    @Resource
    private StringRedisTemplate stringRedisTemplate;
//
//
//    @Override
//    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
//
//        HttpServletRequest request = (HttpServletRequest) servletRequest;
//        HttpServletResponse response = (HttpServletResponse) servletResponse;
//        String username = request.getParameter("username");
//        if (StrUtil.isBlank(username)){
//            filterChain.doFilter(request, response);
//            return;
//        }
//        System.out.println("username" + username);
//        String password = request.getParameter("password");
//        String ASEKey = AES_KEY_KEY + username;
//        String RSA_privateKey = RSA_PRIVATE_KEY + username;
//        System.out.println("key" +request.getHeader("key"));
//        System.out.println("ASEKey" + ASEKey);
//        stringRedisTemplate.opsForValue().set("test", "123456");
//        stringRedisTemplate.opsForValue().set(ASEKey, request.getHeader("key"), AES_KEY_TTL, TimeUnit.MINUTES);
//        stringRedisTemplate.opsForValue().set(RSA_privateKey, request.getHeader("privateKey"), RSA_PRIVATE_TTL, TimeUnit.MINUTES );
//        System.out.println("username" + username);
//        System.out.println("password" + password);
//        filterChain.doFilter(request, response);
//    }

    public String encryptUsername(String contend) throws Exception{
        String key = stringRedisTemplate.opsForValue().get(AES_KEY_KEY + contend);
        String privateKey = stringRedisTemplate.opsForValue().get(RSA_PRIVATE_KEY + contend);
        System.out.println(contend);
        //用私钥解密到AES的key
        System.out.println(privateKey);
        byte[] plaintext = decryptByPrivateKey(Base64.decodeBase64(contend), privateKey);
        contend = new String(plaintext);
        System.out.println("RSA 解密之后:");
        System.out.println(contend);
        //AES解密得到明文data数据
        String result = AESUtil.decrypt(contend, key);
        System.out.println("AES 解密之后:");
        System.out.println(result);
        return contend;
    }
}
